from rest_framework import serializers

from .models import meaus


class Children2MeausSerializer(serializers.ModelSerializer):
    class Meta:
        model = meaus
        fields = ['id', 'name', 'level', 'path']

class ChildrenMeausSerializer(serializers.ModelSerializer):
    children = Children2MeausSerializer(many=True, read_only=True)
    class Meta:
        model = meaus
        fields = ['id', 'name', 'level', 'path', 'children']

class MeausSerializer(serializers.ModelSerializer):
    # 通过related_name='children'指定子级数据的反向查询字段名为children
    children = ChildrenMeausSerializer(many=True, read_only=True)
    class Meta:
        model = meaus
        fields = ['id', 'name', 'level', 'path', 'children']